//
//  YCSelectCarStyleViewController.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/1.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  选择车型

#import "YCSelectCarStyleViewController.h"
#import "YCSelectClassStyleViewController.h"
#import "YCCarStyleCell.h"
#import "YCSelectCarModel.h"
#import "YCFillInfomationModel.h"

@interface YCSelectCarStyleViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 表  */
@property (nonatomic, strong, readonly) UITableView *tableView;
/** 底部条  */
@property (nonatomic, strong, readonly) UIView *bottomView;
/** 下一步  */
@property (nonatomic, strong) UIButton *nextStep;
/** 数据  */
@property (nonatomic, strong) NSArray *dataArray;
/** 填写资料的模型  */
@property (nonatomic, strong) YCFillInfomationModel *fillInfoModel;
@end

@implementation YCSelectCarStyleViewController
@synthesize tableView = _tableView;
@synthesize bottomView = _bottomView;


#pragma mark - 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        CGRect rect = CGRectMake(0, 0, self.view.yc_width, self.view.yc_height - 64-50);
        _tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.contentInset = UIEdgeInsetsMake(12, 0, 0, 0);//18 = 12 + 6
        _tableView.rowHeight = 135;// 123+间距12(上下各6)
    }
    return _tableView;
}

// 底部条
- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        UIButton *nextStep = [[UIButton alloc] init];
        [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
        [nextStep setTitleColor:[UIColor yc_colorWithUInt:0x4766ab] forState:UIControlStateNormal];
        [nextStep setTitleColor:[UIColor yc_colorWithUInt:0xa1a1a1] forState:UIControlStateDisabled];
        nextStep.titleLabel.font = [UIFont systemFontOfSize:16];
        [nextStep addTarget:self action:@selector(didTouchUpInsideNextStepButton:) forControlEvents:UIControlEventTouchUpInside];
        nextStep.enabled = NO;
        self.nextStep = nextStep;
        [_bottomView addSubview:nextStep];
        
        [nextStep mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bottomView).offset(-33);
            make.centerY.equalTo(_bottomView);
        }];
    }
    return _bottomView;
}

- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
- (YCFillInfomationModel *)fillInfoModel
{
    if (!_fillInfoModel) {
        _fillInfoModel = [[YCFillInfomationModel alloc] init];
    }
    return _fillInfoModel;
}

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    [self yc_addBackBarButtonItem];
    self.navigationItem.title = @"选择车型";
    
    [self makeView];
    
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 改变导航栏颜色
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setShadowImage:[UIImage yc_imageWithColor:[UIColor clearColor] size:CGSizeMake(kScreenWidth, 1)]];
    [bar setBackgroundImage:[UIImage yc_imageWithColor:[UIColor yc_colorWithUInt:0xf7f7f7] size:CGSizeMake(kScreenWidth, 64)] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 还原导航条背景
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setShadowImage:[UIImage yc_imageWithColor:[UIColor clearColor] size:CGSizeMake(kScreenWidth, 1)]];
    [bar setBackgroundImage:[UIImage yc_imageWithColor:[UIColor whiteColor] size:CGSizeMake(kScreenWidth, 64)] forBarMetrics:UIBarMetricsDefault];
}

- (void)makeView
{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 数据处理
- (void)loadData
{
    
    NSDictionary *params = @{
                              @"uid":@"1",
                              @"token":accessToken_Test,
                              };
    [[YCNetworManager shareManager] postWithUrl:YCAPISelectCarType params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        self.dataArray = [YCSelectCarModel yc_objectArrayWithKeyValuesArray:response.data];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [self.tableView.mj_header endRefreshing];
    }];
}


#pragma mark - UITableView dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCCarStyleCell *cell = [YCCarStyleCell tableViewCellWithTableView:tableView withIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableView delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    YCSelectCarModel *model = self.dataArray[indexPath.row];
    for (YCSelectCarModel *obj in self.dataArray) {
        if (obj == model) {
            model.selected = !model.isSelected;
        } else {
            obj.selected = NO;
        }
    }
    [self.tableView reloadData];
    // 遍历所有,如果没一个选中,那么就下步一无效
    for (YCSelectCarModel *model in self.dataArray) {
        if (model.isSelected) {
            self.nextStep.enabled = YES;//下一步有效
            self.fillInfoModel.cartypeid = model.cartypeid;
            self.fillInfoModel.cartypeName = model.title;
            break;
        } else {
            self.nextStep.enabled = NO;
        }
    }
}

#pragma mark - 监听点击事件

- (void)didTouchUpInsideNextStepButton:(UIButton *)button
{
    YCSelectClassStyleViewController *selectClassVC = [[YCSelectClassStyleViewController alloc] initWithFillInfoModel:self.fillInfoModel];
    [self.navigationController pushViewController:selectClassVC animated:YES];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
